#open(WRBUFF,"|gzip >file_impute_10000genome_reference_complete_SNPs_only.gz");
#open(WRBUFF1,"|gzip >file_impute_10000genome_reference_complete_INDELs_only.gz");
#$reference = "/data4/bsi/refdata/genetics/impute/ALL_1000G_phase1integrated_feb2012_impute";

open(WRBUFF,"|gzip >file_impute_10000genome_reference_maf0.005_SNPs_only.gz");
open(WRBUFF1,"|gzip >file_impute_10000genome_reference_maf0.05_INDELs_only.gz");
$reference = "./impute_ref_maf0_005p_indels";

print WRBUFF "Id\tchrom\tpos\tallele1\tallele2\n";
print WRBUFF1 "Id\tchrom\tpos\tallele1\tallele2\n";

for($ch = 1;$ch<24;$ch++)
{
        if($ch ==23)
        {
                $refer_1000 = "$reference/ALL_1000G_phase1integrated_feb2012_chrX_nonPAR_impute.legend.gz";
		#$ch = "X";
        }
        else
        {
                $refer_1000 = "$reference/ALL_1000G_phase1integrated_feb2012_chr$ch"."_impute.legend.gz";
        }
        print "ref $refer_1000\n";
        open(RBUFF,"gunzip -c $refer_1000|") or die "Can't open $refer_1000 : $!";
        $line = <RBUFF>;
        #loading hapmap snps in to tha hash
        while($line = <RBUFF>)
        {
             $line =~ s/\n//g;
             $line =~ s/\r//g;
             @array = split(" ",$line);
	     if($array[4] eq "SNP")
	     {  
		if($ch ==23)
		{		
	     		print WRBUFF "$array[0]\tX\t$array[1]\t$array[2]\t$array[3]\n";		
		}
		else
		{
			print WRBUFF "$array[0]\t$ch\t$array[1]\t$array[2]\t$array[3]\n";
		}
	     }
	     elsif($array[4] eq "INDEL")
	    {
			if($ch ==23)
                	{
                        	print WRBUFF1 "$array[0]\tX\t$array[1]\t$array[2]\t$array[3]\n";
                	}
                	else
                	{
                        	print WRBUFF1 "$array[0]\t$ch\t$array[1]\t$array[2]\t$array[3]\n";
                	}
	    }
	    	else
	 	{
			#die "$line\n";
		}					
	}
}
